<template>
  <div v-if="showButton">
    <el-button
      type="primary"
      @click="showDialog"
      :loading="printLoading"
      :disabled="this.selectOrderId.length <= 0"
    >
      {{$t('order.generatePickingList')}}
    </el-button>
    <el-dialog
      :title="$t('选择物流商')"
      :visible.sync="dialog"
      width="500px"
      style="text-align: left;"
    >
      <template>
        <el-radio
          v-for="(item, index) in integratedLogisticsSettingData"
          :key="index"
          v-model="getDefaultLogisticsType"
          :label="item.logistics_type"
        >
          {{ item.logistics_type }}
        </el-radio>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">{{$t('common.cancel')}}</el-button>
        <el-button type="primary" @click="batchOrderCreatePicking(getDefaultLogisticsType)" :loading="loading">{{ $t('common.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {createPicking} from "@/api/order";

export default {
  name: 'LogisticsBatchOrderCreatePickingButton',
  props:{
    accountTypeNum: {
      type: Number,
    },
    logisticType: {
      type: String,
    },
    integratedLogisticsSettingData: {
      type: Array,
    },
    selectOrderId: {
      type: Array,
    },
    defaultLogisticsType: {
      type: String,
    },
  },
  computed: {
    showButton(){
      // if (this.integratedLogisticsSettingData.length <= 0) {
      //   return false
      // }
      let flag = false
      if (this.accountTypeNum === 1) {
        if (this.logisticType === 'custom') {
          flag = true
        }
      }
      if (this.accountTypeNum === 4) {
        if (this.logisticType === 'logisticsSheet') {
          flag = true
        }
      }
      if (this.accountTypeNum === 7) {
        flag = true
      }
      return flag
    },
  },
  data(){
    return {
      dialog: false,
      loading: false,
      printLoading: false,
      getDefaultLogisticsType: ''
    }
  },
  created() {
    this.getDefaultLogisticsType = this.defaultLogisticsType
  },
  methods: {
    showDialog(){
      this.batchOrderCreatePicking()
      // this.getDefaultLogisticsType = this.defaultLogisticsType
      // if (this.integratedLogisticsSettingData.length <= 0) {
      //   return false
      // } else if (this.integratedLogisticsSettingData.length === 1) {
      //   this.batchOrderCreatePicking()
      // } else {
      //   this.dialog = true
      // }
    },

    // 生成拣货单
    batchOrderCreatePicking(logisticsType = ''){
      const postData = {
        orderIds: this.selectOrderId,
        accountTypeNum: this.accountTypeNum,
        logisticsType: logisticsType,
      }
      this.printLoading = true
      this.loading = true
      createPicking(postData)
        .then(res=>{
          this.printLoading = false
          this.loading = false
          if(res.error !== 0){
            this.$errorMessage(res.message)
          }else{
            const pickingNo = res.data.pickingNo
            let routeUrl = this.$router.resolve({
              name: 'packInfo',
              query: {
                'pickingNo' : pickingNo,
              }
            })
            // 新标签打开路由
            window.open(routeUrl.href, '_blank');
            // 刷新当前页面
            this.$router.go(0)
          }
        })
    },
  },

}

</script>

<style scoped lang="scss">

</style>
